﻿using System;
using System.IO;
using System.Text;

namespace AK.Core
{
    public static class FileUtil
    {

        public static Uri FileNameToUri(string fileName)
        {
            AkDebug.AssertArgumentNullOrEmpty(fileName, "fileName", null);

            Uri uri = new Uri(fileName);
            return uri;
        }

        /// <summary>
        /// 将数据保存到相应的文件中
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="content">需要保存的数据</param>
        /// <param name="encoding">文件的编码形式</param>
        public static void SaveFile(string fileName, string content, Encoding encoding)
        {
            AkDebug.AssertArgumentNullOrEmpty(fileName, "fileName", null);
            AkDebug.AssertArgumentNull(content, "content", null);
            AkDebug.AssertArgumentNull(encoding, "encoding", null);

            FileStream file = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.Read);
            using (TextWriter writer = new StreamWriter(file, encoding))
            {
                writer.Write(content);
            }
        }

        public static void SaveFile(string fileName, string content)
        {
            SaveFile(fileName, content, Encoding.Default);
        }

        public static void ConfirmPath(string path)
        {
            AkDebug.AssertArgumentNullOrEmpty(path, "path", null);

            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
        }

        public static void VerifySaveFile(string fileName, string content, Encoding encoding)
        {
            AkDebug.AssertArgumentNullOrEmpty(fileName, "fileName", null);

            string path = Path.GetDirectoryName(fileName);
            ConfirmPath(path);
            SaveFile(fileName, content, encoding);
        }

        public static void VerifySaveFile(string fileName, string content)
        {
            VerifySaveFile(fileName, content, Encoding.Default);
        }

        internal static string GetXmlFilePattern(string pattern)
        {
            if (string.IsNullOrEmpty(pattern))
                return "*.xml";
            AkDebug.Assert(pattern.EndsWith(".xml", StringComparison.OrdinalIgnoreCase),
                string.Format(ObjectUtil.SysCulture, "{0}应该是以.xml结束，而现在不是", pattern), null);
            return pattern;
        }

        public static string ChangeFileName(string path, string newFileNameTitle)
        {
            string dirPath = Path.GetDirectoryName(path);
            string oldFileNameTitle = Path.GetFileNameWithoutExtension(path);

            string _ext = Path.GetExtension(path);

            return Path.Combine(dirPath, newFileNameTitle + _ext);
        }

        public static string ChangeFileNameAddSize(string path, string addSize)
        {
            if (!addSize.IsEmpty())
            {
                string _name = Path.GetFileNameWithoutExtension(path);
                string newName = "{0}_{1}".AkFormat(_name, addSize);
                return ChangeFileName(path, newName);
            }
            else
                return path;
        }

        public static string ReadStringByFile(string fileName)
        {
            FileStream myStream = new FileStream(fileName, FileMode.Open);
            StreamReader myStreamReader = new StreamReader(myStream);
            string fileContent = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            return fileContent;
        }

        public static void CopyDirectory(String sourcePath, String destinationPath)
        {
            DirectoryInfo info = new DirectoryInfo(sourcePath);
            Directory.CreateDirectory(destinationPath);
            foreach (FileSystemInfo fsi in info.GetFileSystemInfos())
            {
                String destName = Path.Combine(destinationPath, fsi.Name);

                if (fsi is System.IO.FileInfo)          //如果是文件，复制文件
                    File.Copy(fsi.FullName, destName);
                else                                    //如果是文件夹，新建文件夹，递归
                {
                    Directory.CreateDirectory(destName);
                    CopyDirectory(fsi.FullName, destName);
                }
            }
        }
    }
}
